package com.ksy.common.login;

import android.content.Context;
import android.text.TextUtils;

/**
 * Created by ksy on 2018/3/27.
 */

public class PhonePlatform implements LoginPlatform<PhoneInfo> {
    private final Context context;
    private Listener listener;

    public interface Listener {
        void onSuccess(PhoneInfo phoneInfo);

        void onError(String msg);

        void onCancel();
    }

    public PhonePlatform(Context context) {
        this.context = context;
    }

    @Override
    public boolean isCanLogin() {
        return true;
    }

    private void onActivityResult(String phone, String password) {
        if (TextUtils.isEmpty(phone.trim())) {
            onError("请输入手机号码");
            return;
        }
        if (TextUtils.isEmpty(password.trim())) {
            onError("请输入密码");
            return;
        }
        PhoneInfo phoneInfo = new PhoneInfo();
        phoneInfo.phone = phone;
        phoneInfo.password = password;
        onSuccess(phoneInfo);
        return;
    }

    public void loginTo(String phone, String password, Listener listener) {
        this.listener = listener;
        onActivityResult(phone, password);
    }

    @Override
    public void onSuccess(PhoneInfo qqInfo) {
        if (listener != null) {
            listener.onSuccess(qqInfo);
        }
    }

    @Override
    public void onError(String msg) {
        if (listener != null) {
            listener.onError(msg);
        }
    }

    @Override
    public void onCancel() {
        if (listener != null) {
            listener.onCancel();
        }
    }
}
